﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.Data.DataFilter;
using Telerik.Windows.Controls.GridView;

namespace ESC.Designer.Resources
{
    public class RadGridViewHelper
    {
        #region GridColumns  
        public static readonly DependencyProperty ColumnsCollectionProperty =
            DependencyProperty.RegisterAttached("ColumnsCollection",
            typeof(GridViewColumnCollection),
            typeof(RadGridViewHelper),
            new PropertyMetadata(ColumnsCollectionsChanged));


        private static void ColumnsCollectionsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            GridViewDataControl grid = d as GridViewDataControl;
            if (grid != null)
            {
                GridViewColumnCollection collection = (GridViewColumnCollection)e.NewValue;
                grid.Columns.Clear();
                foreach (GridViewColumn item in collection)
                {
                    grid.Columns.Add(item);
                }
                //foreach (System.ComponentModel.ItemPropertyInfo obj in grid.Items.ItemProperties)
                //{
                //    obj.set.PropertyType = typeof(string);
                //}
            }
        }

        public static void SetColumnsCollection(DependencyObject d, GridViewColumnCollection columns)
        {
            d.SetValue(ColumnsCollectionProperty, columns);
        }

        public static GridViewColumnCollection GetColumnsCollection(DependencyObject d)
        {
            return (GridViewColumnCollection)d.GetValue(ColumnsCollectionProperty);
        }
        #endregion  


        #region GridColumns  
        public static readonly DependencyProperty ItemPropertyCollectionProperty =
            DependencyProperty.RegisterAttached("ItemPropertyCollection",
            typeof(ItemPropertyDefinitionCollection),
            typeof(RadGridViewHelper),
            new PropertyMetadata(ItemPropertyCollectionChanged));


        private static void ItemPropertyCollectionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            //GridViewDataControl grid = d as GridViewDataControl;
            RadDataFilter grid = d as RadDataFilter;
            if (grid != null)
            {
                ItemPropertyDefinitionCollection collection = (ItemPropertyDefinitionCollection)e.NewValue;
                grid.ItemPropertyDefinitions.Clear();
                foreach (ItemPropertyDefinition item in collection)
                {
                    grid.ItemPropertyDefinitions.Add(item);
                }
            }
        }

        public static void SetItemPropertyCollection(DependencyObject d, ItemPropertyDefinitionCollection columns)
        {
            d.SetValue(ColumnsCollectionProperty, columns);
        }

        public static ItemPropertyDefinitionCollection GetItemPropertyCollection(DependencyObject d)
        {
            return (ItemPropertyDefinitionCollection)d.GetValue(ColumnsCollectionProperty);
        }
        #endregion 
    }
}
